//////////////////////////////////////////////////
// EASIM project
// easim.win.ToolsMenu
// 6/19/2007 5:10 PM
// Cong Liu
// gzcong@gmail.com

// Copyright (C) 2007
// All rights reserved.
// Refer to LICENSE of JIST (Java In Simulation Time) for terms and conditions of use.

package easim.win;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

import jist.runtime.JistAPI.DoNotRewrite;
import jist.swans.Constants;

import easim.net.Field;
import easim.net.Node;

public class ToolsMenu extends JMenu implements DoNotRewrite
{

  public int numberOfNodes = 50;

	public Frame frame;

	public ToolsMenu(String text, Frame frame) {
		super(text);
		this.frame = frame;
    JMenuItem deleteAllNodes = new JMenuItem("Delete All Nodes");
		this.add(deleteAllNodes);
		deleteAllNodes.addActionListener(new DeleteAllNodesActionListener());
		JMenuItem addNodes = new JMenuItem("Add Nodes");
		this.add(addNodes);
		addNodes.addActionListener(new AddNodesActionListener());
	}

	public boolean getNumber() {
		String input = (String)JOptionPane.showInputDialog(frame.fieldPane, 
			"Number of Nodes", "Add Nodes",
			JOptionPane.QUESTION_MESSAGE, null, null, "" + numberOfNodes);
		if (input == null) return false;
		try {
			int number = Integer.parseInt(input);
			if (number <= 0 || number > 1000) {
				throw new NumberFormatException("number <= 0 || number > 1000");
			}
			this.numberOfNodes = number;
			return true;
		} catch (NumberFormatException ex) {
			JOptionPane.showMessageDialog(frame, ex.getMessage(), 
				"ERROR", JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}

  protected class Interactor1 implements SynControler.Interactor, DoNotRewrite
  {
    public void interactorAction() {
      frame.fieldPane.nodeFactory.reset();
      Field.NODES = 0;
      frame.fieldPane.repaint();
    }
  }

  protected class DeleteAllNodesActionListener implements ActionListener, DoNotRewrite {
    public void actionPerformed(ActionEvent e) {
      SynControler.deleteAllNodes();
      SynControler.interact(new Interactor1());
    }
  }

  protected class Interactor2 implements SynControler.Interactor, DoNotRewrite
  {
    public void interactorAction() {
      for (int i = 0; i < numberOfNodes; ++ i) {
        Node node = frame.fieldPane.nodeFactory.create(0);
        int x = Constants.random.nextInt(frame.fieldPane.getWidth());
        int y = Constants.random.nextInt(frame.fieldPane.getHeight());
        SynControler.addNode(node, x, y);
      }
      Field.NODES += numberOfNodes;
      System.out.println("Field.NODES = " + Field.NODES);
      frame.fieldPane.repaint();
    }
  }

  protected class AddNodesActionListener implements ActionListener, DoNotRewrite {
    public void actionPerformed(ActionEvent e) {
      if (! getNumber()) return;
      SynControler.interact(new Interactor2());
    }
  }

}
